/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018/11/21 Time: 10:03
 *
 * 工作
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    Image,
    ScrollView,
    TouchableOpacity,
    TouchableHighlight, Platform, BackHandler, ToastAndroid
} from 'react-native';
import {connect} from 'react-redux';
import {styles} from './JobScreen.Style';
import OffWorkView from "../../../component/OffWorkView/OffWorkView"
import HS_TouchableHighlight from "../../../hsstyle/HS_TouchableHighlight";
import {QmsRecordSummary} from "./Actions";
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import {doDisplayWarnMessage} from "../../../component/Message/Action";
import User from "../../../model/User/User";

class JobScreen extends Component {

  static navigationOptions = ({navigation}) => ({
    headerTitle: `工作`,
    headerLeft: <View/>,
    headerRight: <OffWorkView navigation={navigation}/>
  });

  // 构造
  constructor(props) {
    super(props);
    // 初始状态
    this.state = {
        RecordSummary:{},
    };
  }

  componentWillReceiveProps(nextProps, nextContext): void {
    let {dispatch} = this.props;
    if (nextProps.onGetRecordSummary !== this.props.onGetRecordSummary && nextProps.onGetRecordSummary.fetchStatus === FetchStatus.Success) {
      if (nextProps.onGetRecordSummary.RETURN === ReturnStatus.Success) {
        let RecordSummary = nextProps.onGetRecordSummary;
        this.setState({
            RecordSummary: RecordSummary,
        });
        console.log(RecordSummary)
      } else {
          dispatch(doDisplayWarnMessage(nextProps.onGetRecordSummary.MSG))
      }
    }
  }

  // 组件渲染后调用
  componentDidMount() {
    // this.props.navigation.navigate('MainNavigator');
    let {dispatch} = this.props;
    let str = {};
    dispatch(QmsRecordSummary(JSON.stringify(str)))
  }

  componentWillMount() {
      if (Platform.OS === 'android'||Platform.OS==='ios') {
          BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
      }
  };

  componentWillUnmount() {
      if (Platform.OS === 'android'||Platform.OS==='ios') {
          BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
      }
  };

  onBackAndroid = () =>{
      if(this.props.navigation.isFocused()){//判断   该页面是否处于聚焦状态
          if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
              BackHandler.exitApp();//直接退出APP
          }else{
              this.lastBackPressed = Date.now();
              ToastAndroid.show('再按一次退出应用', 1000);//提示
              return true;
          }
      }
  };

  render() {
    let {dispatch} = this.props;
    return (
      <SafeAreaView style={styles.container}>

        <ScrollView style={styles.scrollView}>

          {/* 我的工单 */}
          {
            <HS_TouchableHighlight  onPress={() => {
              // this.props.navigation.navigate('WorkOrderScreen')

                User.getButtonRight(buttonRight=>{
                    if (buttonRight && buttonRight.buttonRight) {
                        let buttonRight_arr = JSON.parse(buttonRight.buttonRight)
                        let ifEnter = 0
                        for(let i = 0;i<buttonRight_arr.length;i++){
                            if(buttonRight_arr[i].button==="WorkOrderScreen" && ( buttonRight_arr[i].jsp==='JobScreen' || buttonRight_arr[i].jsp==='*' )){
                                ifEnter = 1
                            }
                        }
                        if(ifEnter===0){
                            this.props.navigation.navigate('WorkOrderScreen')
                        }else{
                            ToastAndroid.show('无我的工单权限',1000)
                        }
                    }
                })
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_wdgd.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      我的工单
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      有{Number(this.state.RecordSummary.myUnComplate)+Number(this.state.RecordSummary.unReview)}张新工单
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }

          {/* 服务管理 */}
          {/*{
            <HS_TouchableHighlight  onPress={() => {
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_fwgl.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      服务管理
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      查看对客服务，赶房，服务排班等
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }*/}

          {/* 客房管理 */}
          {/*{
            <HS_TouchableHighlight  onPress={() => {
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_kfgl.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      客房管理
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      分配清洁，查看清洁进度，房态图等
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }*/}

          {/* 工程管理 */}
          {
            <HS_TouchableHighlight id={'EngineerManagementScreen'}  onPress={() => {
                // this.props.navigation.navigate('EngineerManagementScreen')

                User.getButtonRight(buttonRight=>{
                    if (buttonRight && buttonRight.buttonRight) {
                        let buttonRight_arr = JSON.parse(buttonRight.buttonRight)
                        let ifEnter = 0
                        for(let i = 0;i<buttonRight_arr.length;i++){
                            if(buttonRight_arr[i].button==="EngineerManagementScreen" && ( buttonRight_arr[i].jsp==='JobScreen' || buttonRight_arr[i].jsp==='*' )){
                                ifEnter = 1
                            }
                        }
                        if(ifEnter===0){
                            this.props.navigation.navigate('EngineerManagementScreen')
                        }else{
                            ToastAndroid.show('无工程管理权限',1000)
                        }
                    }
                })
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_gcgl.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      工程管理
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      有{this.state.RecordSummary.unDispatch}张未派工单
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }

          {/* 培训课程 */}
          {/*{
            <HS_TouchableHighlight  onPress={() => {
                dispatch(doDisplayWarnMessage("正在开发中..."))
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_pxkc.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      培训课程
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      提供丰富的在线学习视频
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }*/}

          {/* 功能建议 */}
         {/* {
            <HS_TouchableHighlight  onPress={() => {
                // this.props.navigation.navigate('FcuntionalSuggestionsScreen')
                dispatch(doDisplayWarnMessage("正在开发中..."))
            }}>
              <View style={styles.cellView}>
                <View style={styles.cellLeftView}>
                  <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_gnjy.png')}/>
                </View>
                <View style={styles.cellRightView}>
                  <View style={styles.cellRightTopView}>
                    <Text style={styles.cellRightTopText}>
                      功能建议
                    </Text>
                  </View>
                  <View style={styles.cellRightBottomView}>
                    <Text style={styles.cellRightBottomText}>
                      遇到问题，给我们反馈
                    </Text>
                  </View>
                </View>
              </View>
            </HS_TouchableHighlight>
          }*/}

            {/* 客房状态 */}
            {
                <HS_TouchableHighlight id={'RoomDiskScreen'} onPress={() => {
                    // this.props.navigation.navigate('FcuntionalSuggestionsScreen')
                    // dispatch(doDisplayWarnMessage("正在开发中..."))
                    // this.props.navigation.navigate('RoomDiskScreen');

                    User.getButtonRight(buttonRight=>{
                        if (buttonRight && buttonRight.buttonRight) {
                            let buttonRight_arr = JSON.parse(buttonRight.buttonRight)
                            let ifEnter = 0
                            for(let i = 0;i<buttonRight_arr.length;i++){
                                if(buttonRight_arr[i].button==="RoomDiskScreen" && ( buttonRight_arr[i].jsp==='JobScreen' || buttonRight_arr[i].jsp==='*' )){
                                    ifEnter = 1
                                }
                            }
                            if(ifEnter===0){
                                this.props.navigation.navigate('RoomDiskScreen');
                            }else{
                                ToastAndroid.show('无客房状态权限',1000)
                            }
                        }
                    })
                }}>
                    <View style={styles.cellView}>
                        <View style={styles.cellLeftView}>
                            <Image style={styles.cellLeftImage} source={require('../../../resource/job/work_kfgl.png')}/>
                        </View>
                        <View style={styles.cellRightView}>
                            <View style={styles.cellRightTopView}>
                                <Text style={styles.cellRightTopText}>
                                    客房状态
                                </Text>
                            </View>
                            <View style={styles.cellRightBottomView}>
                                <Text style={styles.cellRightBottomText}>
                                    {/*遇到问题，给我们反馈*/}
                                </Text>
                            </View>
                        </View>
                    </View>
                </HS_TouchableHighlight>
            }

        </ScrollView>

      </SafeAreaView>
    )
  }
}

const props = state => {
  return {
      onGetRecordSummary: state.onGetRecordSummary,
  }
};

export default JobScreen = connect(props)(JobScreen);
